package com.sld.concrete.register.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.sld.concrete.algorithm.vo.WorkDataInfo;
import com.sld.concrete.device.domain.RelateDeviceVehicle;
import com.sld.concrete.device.mapper.RelateDeviceVehicleMapper;
import com.sld.concrete.vehicle.domain.RecDevWorkSequence;
import com.sld.concrete.vehicle.mapper.RecDevWorkSequenceMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.sld.concrete.register.mapper.InfoConcreteRegisterMapper;
import com.sld.concrete.register.domain.InfoConcreteRegister;
import com.sld.concrete.register.service.IInfoConcreteRegisterService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 混凝土信息登记Service业务层处理
 * 
 * @author topviewers
 * @date 2022-04-23
 */
@Slf4j
@Service
public class InfoConcreteRegisterServiceImpl implements IInfoConcreteRegisterService 
{
    @Resource
    private InfoConcreteRegisterMapper infoConcreteRegisterMapper;

    @Resource
    RecDevWorkSequenceMapper sequenceMapper;

    @Resource
    RelateDeviceVehicleMapper relateDeviceVehicleMapper;

    /**
     * 查询混凝土信息登记
     * 
     * @param registerId 混凝土信息登记主键
     * @return 混凝土信息登记
     */
    @Override
    public InfoConcreteRegister selectInfoConcreteRegisterByRegisterId(Long registerId)
    {
        return infoConcreteRegisterMapper.selectInfoConcreteRegisterByRegisterId(registerId);
    }

    /**
     * 查询混凝土信息登记列表
     * 
     * @param infoConcreteRegister 混凝土信息登记
     * @return 混凝土信息登记
     */
    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<InfoConcreteRegister> selectInfoConcreteRegisterList(InfoConcreteRegister infoConcreteRegister)
    {
        return infoConcreteRegisterMapper.selectInfoConcreteRegisterList(infoConcreteRegister);
    }

    /**
     * 新增混凝土信息登记
     * 
     * @param infoConcreteRegister 混凝土信息登记
     * @return 结果
     */
    @Override
    public int insertInfoConcreteRegister(InfoConcreteRegister infoConcreteRegister) throws Exception {
        String plateNo = infoConcreteRegister.getPlateNo();
        if (StringUtils.isEmpty(plateNo)) {
            throw new Exception("车牌信息不能为空");
        }
        //1、没有绑定信息，直接返回
        List<RelateDeviceVehicle> relateVehicles = relateDeviceVehicleMapper.queryBindInfo(plateNo);
        if (relateVehicles == null && relateVehicles.size() == 0) {
            throw new Exception("该车辆并没有录入到系统，请联系平台运营。");
        }

        //2、查询该车牌是否已经存在未绑定的记录
        List<InfoConcreteRegister> list = infoConcreteRegisterMapper.queryByBindState(plateNo, "0");
        if (list != null && list.size() > 0) {
            throw new Exception("该车辆已有登记，不能重复创建。原因：信息登记后，该车没有运输数据上报。");
        }
        //3、如果该车正在运输中，需要走补录，不能新增登记信息。
        RecDevWorkSequence runningSequence = sequenceMapper.queryRunningRecord(plateNo);
        if (runningSequence != null && runningSequence.getRegisterId() == null) {
            throw new Exception("该车辆正在运输中，请点击补录，选择对应车次并补充登记信息");
        }

        SysUser sysUser = ShiroUtils.getSysUser();
        infoConcreteRegister.setCreateUser(sysUser.getUserName());
        infoConcreteRegister.setDeptId(sysUser.getDeptId().intValue());
        infoConcreteRegister.setInsertTime(new Date());
        infoConcreteRegister.setBindState("0");
        return infoConcreteRegisterMapper.insertInfoConcreteRegister(infoConcreteRegister);
    }

    /**
     * 修改混凝土信息登记
     * 
     * @param infoConcreteRegister 混凝土信息登记
     * @return 结果
     */
    @Override
    public int updateInfoConcreteRegister(InfoConcreteRegister infoConcreteRegister)
    {
        return infoConcreteRegisterMapper.updateInfoConcreteRegister(infoConcreteRegister);
    }

    /**
     * 批量删除混凝土信息登记
     * 
     * @param registerIds 需要删除的混凝土信息登记主键
     * @return 结果
     */
    @Override
    public int deleteInfoConcreteRegisterByRegisterIds(String registerIds)
    {
        return infoConcreteRegisterMapper.deleteInfoConcreteRegisterByRegisterIds(Convert.toStrArray(registerIds));
    }

    /**
     * 删除混凝土信息登记信息
     * 
     * @param registerId 混凝土信息登记主键
     * @return 结果
     */
    @Override
    public int deleteInfoConcreteRegisterByRegisterId(Long registerId)
    {
        return infoConcreteRegisterMapper.deleteInfoConcreteRegisterByRegisterId(registerId);
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<WorkDataInfo> queryUnbindSequence(WorkDataInfo workData) {
        return sequenceMapper.queryUnbindSequence(workData);
    }

    @Override
    public int saveSequenceRegisterInfo(String seqId, String plateNo, String concreteType, int concreteVolume,
                                        String companyName, String mixingStation, String machineUnit,
                                        int slumpStart, int slumpEnd) throws Exception{
        RecDevWorkSequence sequence = sequenceMapper.selectRecDevWorkSequenceById(Long.parseLong(seqId));
        if (sequence.getRegisterId() != null) {
            throw new Exception("该运输任务已经绑定登记了，无效的操作，您刷新一下试试？");
        }

        InfoConcreteRegister infoConcreteRegister = new InfoConcreteRegister();
        infoConcreteRegister.setCompanyName(companyName);
        infoConcreteRegister.setMixingStation(mixingStation);
        infoConcreteRegister.setMachineUnit(machineUnit);
        infoConcreteRegister.setPlateNo(plateNo);
        infoConcreteRegister.setConcreteType(concreteType);
        infoConcreteRegister.setConcreteVolume(concreteVolume);
        infoConcreteRegister.setSlumpStart(slumpStart);
        infoConcreteRegister.setSlumpEnd(slumpEnd);
        infoConcreteRegister.setBindState("1");
        infoConcreteRegister.setBindTime(new Date());
        infoConcreteRegister.setWorkSeqId(Integer.parseInt(seqId));
        infoConcreteRegister.setInsertTime(new Date());
        infoConcreteRegister.setWorkDate(sequence.getWorkDate());

        infoConcreteRegisterMapper.insertInfoConcreteRegister(infoConcreteRegister);
        long registerId = infoConcreteRegister.getRegisterId();
        sequence.setRegisterId(registerId);
        sequenceMapper.updateRecDevWorkSequence(sequence);
        log.info("【补录】 登记信息入库成功，双向绑定成功。regId={}, seqId={}", registerId, seqId);
        return 1;
    }
}
